<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证码</title>
    <style>
        svg{
            border:1px solid red;
        }
    </style>
</head>
<body>
    <svg id="code" xmlns="http://www.w3.org/2000/svg" version="1.1" width="120" height="40">

    </svg>
    <script>
        function random(start,end){
            if(!end){
                return parseInt(Math.random() * (start + 1))
            }else{
                return parseInt(Math.random() * (end - start + 1)) + start;
            }
        }
        let codeSvg = document.getElementById("code");
            let letter = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            let genderateCode = function(){
                let codeStr = "";
                for(let i = 0;i < 4;i++){
                    let index = random(letter.length - 1);
                    codeStr += `<tspan x="${i*20 + random(5,20)}" y="${random(10,30)}" fill="rgb(${random(255)},${random(255)},${random(255)})" rotate="${random(-45,45)}">${letter.charAt(index)}</tspan>`;
                }
                codeStr = `<text>${codeStr}</text>`;
                for(let i = 0;i < 10;i++){
                    codeStr += `<line x1="${random(120)}" y1="${random(40)}" x2="${random(120)}" y2="${random(40)}" stroke="rgb(${random(255)},${random(255)},${random(255)})"></line>`;
                }
                console.log(codeStr);
                codeSvg.innerHTML = codeStr;
            }
            genderateCode();
    </script>
</body>
</html>